package net.fdukedom.epicurus.presentation.actions.user;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import net.fdukedom.epicurus.presentation.actions.AbstractForm;
import net.fdukedom.epicurus.presentation.SessionData;
import net.fdukedom.epicurus.domain.entity.User;

/**
 * Bean for user profile form.
 * 
 * @author Alexander Kirakozov
 * @author Dmitry Y. Kochelaev
 */
public class ProfileForm extends AbstractForm {

    /**
     * User name.
     */
    private String userName;

    /**
     * E-mail.
     */
    private String email;

    /**
     * Password.
     */
    private String password;

    /**
     * User sex.
     */
    private Short sex;

    @Override
    public void doReset(ActionMapping mapping, HttpServletRequest request) {
        User user = SessionData.getInstance(request).getUser();
        userName = user.getName();
        email = user.getEmail();
        password = "";
        sex = user.getSex();
    }

    @Override
    public ActionErrors doValidate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (userName.length() == 0) {
            if (userName.length() == 0) {
                errors.add("error.profile.login", new ActionMessage("profile.errors.emptyLogin"));
            }
        }
        return errors;
    }

    /**
     * Returns user name.
     *
     * @return root login
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Sets user name.
     *
     * @param userName user name
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * Returns user email.
     *
     * @return user email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Empty method. Just for compatibility with bean interface. It does not
     * modify user entity anyhow.
     *
     * @param email user email
     */
    public void setEmail(String email) {
    }

    /**
     * Returns empty string. The password is not to be shown on login page.
     *
     * @return empty string
     */
    public String getPassword() {
        return "";
    }

    /**
     * Returns user password.
     *
     * @return user password
     */
    public String getNonEmptyPassword() {
        return password;
    }

    /**
     * Sets user password.
     *
     * @param password user password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Returns user sex.
     * 
     * @return user sex
     */
    public Short getSex() {
        return sex;
    }

    /**
     * Sets user sex.
     * 
     * @param sex user sex
     */
    public void setSex(Short sex) {
        this.sex = sex;
    }
}
